package com.ssyc.student.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ssyc.common.manager.PopUpManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.student.R;
import com.ssyc.student.bean.StTreasuerFood;
import com.ssyc.student.bean.UserFoodItemJson;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生端百宝箱食物,时装列表适配器
 * Created by Administrator on 2018/4/12 0012.
 */

public class StTreasuerFoodAdapter extends BaseQuickAdapter<StTreasuerFood.DataBean, BaseViewHolder> {
    private Context context;

    //屏幕宽度
    private int screenWidth;

    //Activity对象
    private Activity activity;

    //Activity对象
    private int count = 1;

    //ImageView的布局参数
    private LinearLayout.LayoutParams lvLp;
    //使用按钮打的布局参数
    private LinearLayout.LayoutParams tvLp;


    public Handler mHandler;

    public StTreasuerFoodAdapter(Activity activity,Context context, int layoutResId,
                                 @Nullable List<StTreasuerFood.DataBean> data,Handler mHandler) {
        super(layoutResId, data);
        this.context = context;
        this.activity = activity;

        this.mHandler = mHandler;
//        screenWidth = ScreenUtils.getScreenWidth(context);
//        //计算条目的宽度
//        int itemSize = (screenWidth - 16 * 2 - 16 - 8 * 4) / 2 - 16;
//        lvLp = new LinearLayout.LayoutParams(itemSize, itemSize);
//        tvLp = new LinearLayout.LayoutParams(itemSize, UiUtils.dp2px(36));
//        tvLp.topMargin = UiUtils.dp2px(6);
    }

    @Override
    protected void convert(BaseViewHolder helper, StTreasuerFood.DataBean item) {

        //物品图片
        ImageView ivImg = helper.getView(R.id.iv_img);

        //物品数量
        TextView tvAmount = helper.getView(R.id.tv_amount);
        //物品名称
        TextView tvName = helper.getView(R.id.tv_name);
        //精力值加成数
        TextView tvEnergy = helper.getView(R.id.tv_energy);
        //Use按钮
        ImageView ivUse = helper.getView(R.id.iv_use);

        //加载图片
        Glide.with(context).load(item.getUrl2()).into(ivImg);
        //设置名称
        tvName.setText(item.getName());
        //设置精力值加成数
        tvEnergy.setText("+"+item.getEnergy());
        //设置数量
        tvAmount.setText(item.getAmount()+"");


        //使用的点击事件
        ivUse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //弹出已经使用的提示框
                shopMessagePop(item);
            }
        });
    }

    /**
     * 弹出是否使用的提示框
     *
     * @param item
     */
    private void shopMessagePop(StTreasuerFood.DataBean item) {
        PopUpManager.showPop(context, R.layout.student_pop_treasuer_food, 0.3f,activity,new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                //标题
                TextView tvTile = view.findViewById(R.id.tv_title);
                tvTile.setText("使用该道具");
//                tvTile.setText("使用"+item.getName()+"?");

                //使用数量
                TextView tvCount = view.findViewById(R.id.tv_food_count);

                //物品图片
                ImageView lvShop = view.findViewById(R.id.lv_shop);

                Glide.with(context).load(item.getUrl()).into(lvShop);

                count = 1;
                tvCount.setText(count + "");

                //减按钮
                ImageView ivMin = view.findViewById(R.id.iv_food_min);
                ivMin.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        //最小不少于1
                        if(count > 1){
                            count--;
                        }
                        tvCount.setText(count + "");

                    }
                });

                //加按钮
                ImageView ivAdd = view.findViewById(R.id.iv_food_add);
                ivAdd.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        //最大不超过拥有数
                        if(count != item.getAmount()){
                            count++;
                        }
                        tvCount.setText(count + "");
                    }
                });

                //关闭图片·
                ImageView lvColse = view.findViewById(R.id.lv_close);
                lvColse.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                    }
                });
                //确定按钮
                Button btBuy = view.findViewById(R.id.bt_buy);
                btBuy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //通过EventBus传值
//                        EventBus.getDefault().post(new BusInfo(item.getGoods_id()));
                        // TODO: 2018/5/3 0003  上传服务器,将购买的信息发送给服务器
//                        UiUtils.Toast("使用"+count+"个"+item.getName()+",增加了 "+(item.getEnergy()*count)+"点精力值", false);

                        List<UserFoodItemJson> list = new ArrayList<>();
                        UserFoodItemJson useFoodJson = new UserFoodItemJson();
                        useFoodJson.setBoxtype(item.getGoods_type()+"");
                        useFoodJson.setBoxitem(item.getGoods_id()+"");
                        useFoodJson.setBoxnum(count+"");
                        useFoodJson.setEnergy(item.getEnergy()+"");
                        useFoodJson.setName(item.getName());

                        list.add(useFoodJson);
                        String userJson = GsonUtil.listToJson(list);

                        Message message = new Message();
                        Bundle bundle = new Bundle();
                        bundle.putString("json",userJson);
                        bundle.putString("Energy",item.getEnergy()+"");
                        message.setData(bundle);
                        mHandler.sendMessage(message);

                        //通知适配器刷新
                        notifyDataSetChanged();
                        PopUpManager.dismiss();
                    }
                });
            }
        });
    }
}
